﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Home : System.Web.UI.Page
{
    Controllers controller;
    List<SanBay> listSanBayDi;
    List<SanBay> listSanBayDen;
    

    protected void Page_Load(object sender, EventArgs e)
    {    
        controller = new Controllers();
        if (!IsPostBack)
        {
            LoadSanBay();
        }
    }



    private void LoadSanBay()
    {
        listSanBayDi = new List<SanBay>();
        
        //load sân bây đi
        listSanBayDi = controller.TimSanBayDi();

        DropDownList1.Items.Clear();
        foreach (SanBay sb in listSanBayDi)
        {
            DropDownList1.Items.Add(sb.TenSanBay);
        }

        listSanBayDen = new List<SanBay>();
        //load sân bay đến
        listSanBayDen = controller.TimSanBayDen();
        DropDownList2.Items.Clear();
        foreach (SanBay sb in listSanBayDen)
        {
            DropDownList2.Items.Add(sb.TenSanBay);
        }
    }


    private string TimMaSanBayTrongList(string tenCB, List<SanBay> listSanBay)
    {
        if (tenCB == null || listSanBay == null)
        {
            return null;
        }

        foreach (SanBay sb in listSanBay)
        {
            if (sb.TenSanBay.Equals(tenCB))
            {
                return sb.MaSanBay;
            }
        }

        return null;
    }
    

    protected void Button2_Click(object sender, EventArgs e)
    {
        string tenSBdi = DropDownList1.SelectedItem.Text;
        listSanBayDi = controller.TimSanBayDi();
        string maSBdi = TimMaSanBayTrongList(tenSBdi, listSanBayDi);
        string tenSBden = DropDownList2.SelectedItem.Text;
        listSanBayDen = controller.TimSanBayDen();
        string maSBden = TimMaSanBayTrongList(tenSBden, listSanBayDen);

        Session.Remove("LoaiChuyenBay");
        Session.Add("LoaiChuyenBay", RadioButtonList2.SelectedValue);
        Session.Remove("IDsanBayDi");
        Session.Add("IDsanBayDi", maSBdi);
        Session.Remove("IDsanBayDen");
        Session.Add("IDsanBayDen", maSBden);
        Session.Remove("TensanBayDi");
        Session.Add("TensanBayDi", tenSBdi);
        Session.Remove("TensanBayDen");
        Session.Add("TensanBayDen", tenSBden);
        Session.Remove("NgayDi");
        Session.Add("NgayDi", TextBox7.Text);
        Session.Remove("NgayDen");
        Session.Add("NgayDen", TextBox8.Text);
        Session.Remove("Adults");
        Session.Add("Adults", DropDownList7.SelectedValue);
        Session.Remove("Children2-12");
        Session.Add("Children2-12", DropDownList8.SelectedValue);
        Session.Remove("Childrenless2");
        Session.Add("Childrenless2", DropDownList9.SelectedValue);

        Response.Redirect("DatVeB2.aspx#divDatVeB");
    }
}